package com.geeklei.media.ui.menu.popup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.geeklei.media.ui.R;

public abstract class PopupListAdapter extends AbsGroupAdapter<ItemWraper> {
    private ItemWraper mCurrentSource;

    public static class ViewHolder {
        public TextView mTextView;
    }

    public abstract void buildView(ViewHolder viewHolder, ItemWraper itemWraper);

    public PopupListAdapter(Context context) {
        super(context);
    }

    public void setCurrentSource(ItemWraper itemWraper) {
        this.mCurrentSource = itemWraper;
        notifyDataSetChanged();
    }
    public void setCurrentSource(int index){
        if(getCount()>index && getItem(index) instanceof ItemWraper){
            this.mCurrentSource = (ItemWraper) getItem(index);
            notifyDataSetChanged();
        }
    }

    public ItemWraper getCurrentSource() {
        return this.mCurrentSource;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null || !(view.getTag() instanceof ViewHolder)) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.vp_list_item_source, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.mTextView = (TextView) view.findViewById(R.id.vp_source_item_text);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (getItem(i) instanceof ItemWraper) {
            ItemWraper itemWraper = (ItemWraper) getItem(i);
            if (this.mCurrentSource != null && this.mCurrentSource.getSource().equals(itemWraper.getSource()) && this.mCurrentSource.getResolution() == itemWraper.getResolution()) {
                viewHolder.mTextView.setSelected(true);
            } else {
                viewHolder.mTextView.setSelected(false);
            }
            buildView(viewHolder, itemWraper);
        }
        return view;
    }
}
